<?php
function_exists('UnixPath')  || die('OK');

	//E::$DB->_tab = '_super_mod';
	$ACT = array('list', 'add', 'aok', 'edit', 'eok', 'del', 'all');
	in_array($_GET['act'], $ACT) || $_GET['act'] = 'list';
	$html['tpl_right_act'] = $_GET['act'];
	$_GET['pid']	= (int) $_GET['pid'];
	$_GET['id']	= (int) $_GET['id'];

	$P = E::$DB->one('_super_mod', 'id='.$_GET['pid']);

	if(!$P['id'] || $P['nodeType'] >=2 ){
		$P = array('id'=>0, 'nodeName'=>'根节点','nodeType'=>'-1');
	}
	//$nodes	= array('模块','功能','动作');
	$tpl->assign('nodes', array('模块','功能','动作'));
	$P['childType']	= $P['nodeType'] +1;


	$body['navlist'] = array(
		'list' => '节点列表',
		'add' => '添加节点',
	);
	$body['_url'] = '&pid=' . $P['id'];
	$body['url'] = $html['url'] . $body['_url'];
	$body['node_tree'] = node_tree($P, 'nodeName' ); 

/**********php cache set*******/
	$order = 'ORDER BY nodeType ASC, pid ASC, orderId DESC, id ASC';
	$cache_file = 'mod';
/****************/

if( $_GET['act'] === 'aok' ){
	$_POST['pid'] = $P['id']; $_POST['nodeType'] = $P['nodeType'] + 1;
	E::$DB->insert('_super_mod', "pid, nodeType, nodeValue, nodeName , orderId, isPower ");
	C::tree('_super_mod', $order, $cache_file);
	mod_cache();
	A::msg('插入节点完成!', $_POST['jurl'], 1, 1 );
}


if( $_GET['act'] === 'edit' ){
	$L = E::$DB->one('_super_mod','id='.$_GET['id']);
	$L['id'] || A::msg('无效的节点', $_POST['jurl'], 1, 1 );
}

if( $_GET['act'] === 'eok' ){
	$L = E::$DB->one('_super_mod', 'id='.$_GET['id']);
	$L['id'] || A::msg('无效的节点', $_POST['jurl'], 1, 1 );
	E::$DB->update('_super_mod', ' nodeValue, nodeName , orderId, isPower', 'id='.$L['id']);
	C::tree('_super_mod', $order, $cache_file);
	mod_cache();
	A::msg('修改节点 '.$_POST['nodeName'].' 完成!', $_POST['jurl'], 1, 1 );
}



if( $_GET['act'] === 'del' ){
	$L = E::$DB->one('_super_mod','id='.$_GET['id']);
	$L['id'] || A::msg('无效的节点', $_POST['jurl'], 1, 1 );
	$x	= E::$DB->all('_super_mod', 'pid='.$L['id'] ) ;
	if( count($x) ) { // 待整理
		A::msg('您必须先删除该节点的子节点!', '',1,1);
	}
	E::$DB->query('DELETE FROM _super_mod where id=' . $L['id'] . ' or pid = '.$L['id']);
	C::tree('_super_mod', $order, $cache_file);
	mod_cache();
	A::msg('删除节点 '.$L['nodeName'].' 完成!', $_POST['jurl'], 1, 1 );
}


if( $_GET['act'] === 'list' ){
	$p = E::$DB->page('_super_mod', 'pid='. $P['id'] );	
	$L = E::$DB->all('_super_mod', 'pid=' . $P['id'] . ' ORDER BY orderId DESC '. $p['limit']);
	if( $P['nodeType'] ==1 ){
		$P['length'] = $p['len'];
	}
}

if( $_GET['act'] === 'all' ){
	if( count(E::$DB->all('_super_mod','pid='.$P['id'] )) || $P['nodeType']!=1 ){
		A::msg('您现在不能使用此功能!', '',1,1);
	}
	$_POST['pid'] = $P['id'];
	$_POST['nodeType'] = $P['nodeType'] + 1;

	$_POST['nodeValue'] = 'list'; $_POST['nodeName'] = '列表';
	E::$DB->insert('_super_mod', 'pid, nodeType, nodeValue, nodeName ');
	$_POST['nodeValue'] = 'add'; $_POST['nodeName'] = '添加';
	E::$DB->insert('_super_mod', 'pid, nodeType, nodeValue, nodeName ');
	$_POST['nodeValue'] = 'aok'; $_POST['nodeName'] = '添加完毕';
	E::$DB->insert('_super_mod', 'pid, nodeType, nodeValue, nodeName ');
	$_POST['nodeValue'] = 'edit'; $_POST['nodeName'] = '编辑';
	E::$DB->insert('_super_mod', 'pid, nodeType, nodeValue, nodeName ');
	$_POST['nodeValue'] = 'eok'; $_POST['nodeName'] = '编辑完毕';
	E::$DB->insert('_super_mod', 'pid, nodeType, nodeValue, nodeName ');
	$_POST['nodeValue'] = 'del'; $_POST['nodeName'] = '删除';
	E::$DB->insert('_super_mod', 'pid, nodeType, nodeValue, nodeName ');

	C::tree('_super_mod', $order, $cache_file);
	mod_cache();
	A::msg("添加节点完成!", '', 1, 1 );
}


function node_tree($p, $key='name'){
	global $html;
	if(!$p['id']){
		return '<a href="'.$html['url'].'&pid=0&act=list">根节点</a>';
	}
	$s = '<a href="'.$html['url'].'&pid='.$p['id'].'&act=list">'.$p[$key].'</a>';
	if( $p['pid'] ) {
		$s = node_tree(E::$DB->one( '_super_mod', 'id='.$p['pid']), $key) .' <small>&#187;</small> '. $s;
	}else{
		$s = '<a href="'.$html['url'].'&pid=0&act=list">根节点</a> <small>&#187;</small> '. $s;
	}
	return $s;
}

function mod_cache(){

	$l	= E::$DB->ary('_super_mod', '1 ORDER BY nodeType ASC, orderId DESC, pid ASC,  id ASC','*','id');
	$L	= array();
	$L2	= array();
	foreach($l as $id=>$v){
		$pid	= $v['pid'];
		$key	= $v['nodeValue'];
		if(!$v['nodeType']){
			$L[$key]	= array();
		}else if($v['nodeType']=='1'){
			$pkey	= $l[$pid]['nodeValue'];
			$L[$pkey][$key]	= array();
			$l[$id]['pkey']	= $pkey;
		}else{
			$pkey	= $l[$pid]['nodeValue'];
			$ppkey	= $l[$pid]['pkey'];
			$L[$ppkey][$pkey][$key]	= $id;
		}
	}
	hash::save($L , B_DIR . '/_res/const/mod.php');
	
	$R	= array();
	foreach($L as $dir => $a){
		foreach($a as $file => $b){
			foreach($b as $act => $id){
				$R[ $dir . '.' . $file  . '.' . $act ]	= $id;
			}
		}
	}
	hash::save($R , B_DIR . '/_res/const/mod2.php');

}
